<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>{{ $t('login.title') }}</h2>
        <p>{{ $t('login.welcome') }}</p>
      </div>

      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @keyup.enter="handleLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            :placeholder="$t('common.username')"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            :placeholder="$t('common.password')"
            prefix-icon="Lock"
            type="password"
            show-password
          />
        </el-form-item>

        <!-- <div class="login-options">
          <el-checkbox v-model="loginForm.remember">{{ $t('login.rememberMe') }}</el-checkbox>
          <el-link type="primary" underline="never">{{ $t('common.forgotPassword') }}</el-link>
        </div> -->

        <el-button
          :loading="loading"
          type="primary"
          class="login-button"
          @click="handleLogin"
        >
          {{ $t('login.loginButton') }}
        </el-button>
      </el-form>

      <!-- <div class="login-footer">
        <div class="language-switch">
          <el-dropdown @command="handleLanguageChange">
            <span class="language-dropdown">
              {{ currentLanguage === 'zh-cn' ? $t('common.chinese') : $t('common.english') }}
              <el-icon><ArrowDown /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="zh-cn">{{ $t('common.chinese') }}</el-dropdown-item>
                <el-dropdown-item command="en-us">{{ $t('common.english') }}</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        
        <div class="theme-switch" @click="toggleTheme">
          <el-icon v-if="currentTheme === 'light'"><Moon /></el-icon>
          <el-icon v-else><Sunny /></el-icon>
          {{ currentTheme === 'light' ? $t('common.darkMode') : $t('common.lightMode') }}
        </div> -->
      <!-- </div> -->
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useI18n } from 'vue-i18n';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage } from 'element-plus';
import { useUserStore } from '@/store/user';
import { userLogin } from '@/api/user';
import { getDictList } from '@/api/dict';
import { useDictStore } from '@/store/dict';

// 国际化
const { t } = useI18n();

// 路由
const router = useRouter();
const route = useRoute();

// 状态管理
const userStore = useUserStore();
const dictStore = useDictStore();
const {
  setUserVip,
  setPayType,
  setOnline,
  setProductType,
  setPayStatus,
  setSysNormalDisable,
  setSysUserSex,
} = dictStore;
// 表单引用
const loginFormRef = ref(null);

// 加载状态
const loading = ref(false);

// 登录表单数据
const loginForm = reactive({
  username: '13434407832',
  password: '123456',
  remember: false,
});

// 表单验证规则
const loginRules = {
  username: [
    {
      required: true,
      message: () => t('login.usernameRequired'),
      trigger: 'blur',
    },
  ],
  password: [
    {
      required: true,
      message: () => t('login.passwordRequired'),
      trigger: 'blur',
    },
  ],
};

// 处理登录
const handleLogin = () => {
  loginFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      userLogin(loginForm.username, loginForm.password)
        .then((res) => {
          ElMessage({
            message: t('login.loginSuccess'),
            type: 'success',
          });
          userStore.setToken(res.data.token);
          getDictList('user_vip').then((res) => {
            if (!res.data) return;
            setUserVip(res.data);
          });
          getDictList('pay_type').then((res) => {
            if (!res.data) return;
            setPayType(res.data);
          });
          getDictList('online').then((res) => {
            if (!res.data) return;
            setOnline(res.data);
          });
          getDictList('product_type').then((res) => {
            if (!res.data) return;
            setProductType(res.data);
          });
          getDictList('pay_status').then((res) => {
            if (!res.data) return;
            setPayStatus(res.data);
          });
          getDictList('sys_normal_disable').then((res) => {
            if (!res.data) return;
            setSysNormalDisable(res.data);
          });
          getDictList('sys_user_sex').then((res) => {
            if (!res.data) return;
            setSysUserSex(res.data);
          });
          const redirect = route.query.redirect || '/web';
          router.push(redirect);
        })
        .catch((err) => {
          console.log(err);
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #ffffff;
  background-image:
    url('@/assets/login/lf_bg.svg'),
    linear-gradient(to bottom, rgba(64, 158, 255, 0.1) 0%, #ffffff 100%);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  transition: background-color 0.3s;
}

.login-box {
  width: 400px;
  padding: 40px;
  border-radius: 8px;
  background-color: var(--sidebar-bg);
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;

  h2 {
    font-size: 24px;
    color: var(--text-color);
    margin-bottom: 10px;
  }

  p {
    font-size: 14px;
    color: var(--info-color);
  }
}

.login-form {
  margin-bottom: 20px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.login-button {
  width: 100%;
  padding: 12px 0;
  font-size: 16px;
  border-radius: 4px;
  transition: all 0.3s;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
  font-size: 14px;
  color: var(--info-color);
}

.language-dropdown,
.theme-switch {
  display: flex;
  align-items: center;
  cursor: pointer;
  transition: color 0.3s;

  &:hover {
    color: var(--primary-color);
  }

  .el-icon {
    margin-right: 5px;
  }
}

/* 响应式适配 */
@media screen and (max-width: 768px) {
  .login-box {
    width: 90%;
    padding: 30px 20px;
  }
}

/* 动画效果 */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-box {
  animation: fadeIn 0.5s ease-out;
}
</style>
